package cn.zhj.springboot.interceptor;

import cn.dev33.satoken.stp.StpUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;


@Component
public class MyInterceptor implements HandlerInterceptor {

    /**
     * Controller 方法执行前调用
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        System.out.println("【Interceptor】preHandle - 请求路径: " + request.getRequestURI());

        // 如果是 Controller 方法
        if (handler instanceof HandlerMethod) {
//            System.out.println("【Interceptor】正在访问的方法: " + ((HandlerMethod) handler).getMethod().getName());
        }

        return true; // 返回 false 则中断请求
    }

    /**
     * Controller 方法执行后调用（视图渲染前）
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//        System.out.println("【Interceptor】postHandle - 请求处理完成");
    }

    /**
     * 整个请求完成后调用（无论是否异常）
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//        System.out.println("【Interceptor】afterCompletion - 请求结束");
    }
}
